/*
**  cms.js 1.0 is edited by jianggc 
**  this is testing, so there may be some problems.
**	please don't modify anything except Jiang
*/
if (typeof AR == "undefined" || !AR) {
	var AR = AR || {}
}
AR = function() {
	this.g = function(D) {
		if ( D.indexOf(" ") == -1){
			return ( D.substring(0,1) === "#") ? document.getElementById(D.substring(1,D.length)) : document.getElementsByTagName(D);
		}
		else {
			var ary = D.split(" ");
			return this.g(ary[0]).getElementsByTagName(ary[1])
		}
	}
}
AR.prototype = {

	trim: function(R) {
		return R.replace(/^\s+|\s+$/g,"")
	},
	cut: function(R,M,N){
		return R = (R.length < N) ? R : R.slice(M,N);
	},
	numAll: function(R,M,N) {
		var rx = /^\d{M,N}$/	;
		return (!E && !rx.exec(E)) ?  false : true;
	},
/*
**  正则验证 
**  strall:全部是字母;chinese:全部是汉字;username:用户名【以字母开头，字母、数字、下划线】;mobile:手机【以1开头的11位数字】;email:邮箱【字母、数字、下划线+@+字母、数字、下划线+.+2-7个字母】
**	R:验证的字符; M:数字量始; N:数字量终;
**  return true[对] or false[错]
**	eg: username("jxiao",5,18) //return true
*/
	strall: function(R,M,N) {
		var rx = eval("(" +"/^[a-zA-Z]{"+M+","+N+"}$/"+")");
		return (!R || !rx.exec(R)) ? false : true;
		
	},
	chinese: function(R,M,N) {
		var rx = eval("(" +"/^[\u4E00-\u9FA5]{"+M+","+N+"}$/"+")");
		return (!R || !rx.exec(R)) ? false : true;
	},
	username: function(R,M,N) {
		var rx = eval("(" +"/^[a-zA-Z][a-zA-z0-9\_]{"+(M-1)+","+(N-1)+"}$/"+")");
		return (!R || !rx.exec(R)) ? false : true;
		
	},
	mobile: function(R) {
		var rx = /^[1]{1}(\d){10}$/;  
		return (!rx.exec(R)) ?  false : true;
	},
	email: function(R) {
		var rx = /^\w+@\w+\.[a-zA-Z]{2,7}$/;
		return (!rx.exec(R)) ? false : true;
	},
	
/*
**  hasClass: 判断某元素下是否有某样式 
**  D:查检元素ID; S:样式名
**  return true[有] or false[无]
**	eg: hasClass("#nodeid","classname")
*/
	hasClass: function(D, S) { 
		var t = this;
		var d = t.g(D), s = t.trim(S), dc = t.trim(d.className);
		if ( !d || !S || !dc) {
			return false;
		}
		if (dc.indexOf(" ") == -1) {
			return (eval("("+"/^"+s+"$/"+")").exec(dc)) ? true : false;
		} else if (dc.indexOf(" ") != -1) {
			var ar = dc.split(" "), i = 0, j = ar.length;
			for (; i < j ; i ++ ) {
				if (eval("("+"/^"+s+"$/"+")").exec(ar[i])) {
					return true;
				}
			}
			return false;
		}
	},
/*
**  addClass: 为某元素附加样式 
**  D:查检元素ID; S:样式名
**	eg: addClass("#nodeid","classname")
*/
	addClass: function(D, S) { 
		var t = this;
		var d = t.g(D);
		if ( !d || !S) {
			return false;
		}
		d.className += " " + S;
	},
	removeClass: function(D, S) { 
		var t = this;
		var d = t.g(D);
		if ( !t.hasClass(D, S)) {
			return;
		}
		d.className = d.className.replace(new RegExp(S,"g"),"");
		
	},
	bodysize: function() {
		var _h = parent.document.documentElement.clientHeight, _h2 = parent.document.body.clientHeight;
		_h = ( _h > _h2 ) ? _h : _h2;
		var ar = [document.body.clientWidth,_h];
		return ar;
	},
	arrdel: function(D,N) {
		if ( !D || N < 0 || isNaN(N)) {
			return;
		}
		else {
			return D.slice(0,N).concat(D.slice(N+1,D.legnth));
		}
	},
	addEvent: function(D, O, U) { 
		var t = this;
		d = t.g(D);
		if ( !d || !O || typeof(U) != "function") {
			return false;
		}
		if ( d.addEventListener) {
			d.addEventListener(O,U,false);
		}
		else if ( d.attachEvent) {
			d.attachEvent("on"+O,U)
		} else {
			d["on" + O] = U;
		}
	},
	removeEvent: function(D,O,U) {
		if ( D.removeEventListener ) {
			D.removeEventListener (D,O,false);
		} else if ( D.attachEvent) {
			D.detachEvent("on"+O,U);
		} else {
			D["on"+O] = U;
		}
	},
	stopEvent: function(v){ 
		if (v.stopPropagation)
		{
			v.stopPropagation();
		}
		else{
			v.cancelBubble = true;
		}
		if (v.preventDefault)
		{
			v.preventDefault();
		} else {
			e.returnValue = false;
		}
	},
	discolor: function(D, S) {
		var t = this;
		if ( !t.g(D) ) {
			return;
		}	
		var d = t.g(D), i = 0, j = d.length;
		for ( ; i < j ; i ++ ) {
			d[i].className = i%2 ? S : "";
		}
	},
	discolor2: function(D, S) {
		var t = this;
		if ( !t.g(D) ) {
			return;
		}	
		var d = t.g(D), i = 0, j = d.length;
		for ( ; i < j ; i ++ ) {
			d[i].className = i%2 ? S : "";
		}
	},
	oncolor: function(D, S) {
		var d = this.g(D), i = 0; j = d.length;
		for (; i < j ; i++ )
		{
			var a;
			d[i].onmouseover = function() {
				a = this.className;
				this.className += " oncolor";
			}
			d[i].onmouseout = function() {
				this.className = a;
			}
		}
	},
	diplays: function (D, C, N, M) {
		if ( !this.g(D) || !this.g(C))	{
			return;
		}
		var m = this.g(D), n = this.g(C)
		n.onlick = function(){
			if ( m.style.display == "none" ) {
				m.style.display = "";
				n.innerHTML = N;
			}else {
				m.style.display = "none";
				n.innerHTML = M;
			}
		}
	},
	tab: function( D, E, F, G) {
		if (!this.g(D) || !this.g(E) || !F || !G)	{
			return;
		}
		var a = this.g(D), b = this.g(E), i = 0, k = a.length;
		for ( ; i < k; i ++ )
		{
			a[i].x = i;
			a[i].onclick = function() {
				var j = 0;
				for ( ; j < k; j ++)
				{
					a[j].className = F;
					b[j].style.display = "none"
				}
				a[this.x].className = G;
				b[this.x].style.display = "block";
			}
		}
	},
/*
**  shade: 遮罩层功能 
**  D:触发按钮ID; E:关闭按钮ID【可多个以“,”间隔】; F:亮层ID; W:亮层宽度【可选，默认500】
**	eg: shade("#showshade","#showshadea,#showshadeb..","#lightdiv"[,600])
*/
	shadez: function(E,F,W) {
		var t = this;
		var w = W ? W : 500;
		t.shadeshowz(F,w);
		t.shadehidden(E,F);
	},
	shadeshowz: function(D,W){
		var t = this;
		var _d = document.createElement("div"), _f = document.createElement("iframe");
		var d = t.g(D), ar = t.bodysize();
		_d.setAttribute("id","shadowbox");
		_d.className = "shadowbox";
		_d.style.cssText = "width:"+ar[0]+"px;height:"+ar[1]+"px;";
		_f.style.cssText = "position:absolute;z-index:-1;width:0px;height:0px;border:none;_width:"+ar[0]+"px;_height:"+ar[1]+"px;filter:alpha(opacity=0);";
		_d.appendChild(_f);
		if (t.g("#shadowbox"))
			t.g("#shadowbox").style.display = "block";
		else
			document.body.appendChild(_d);
		d.style.cssText = "overflow:hidden;width:"+W+"px;height:auto;left:50%;top:150px;display:block;margin-left:-"+W/2+"px";
	},
	shade: function(D,E,F,W) {
		var t = this;
		if (!t.g(D) || !E || !t.g(F)) {
			return;
		}
		var w = W ? W : 500;
		t.addEvent(D,"click",function(){t.shadeshow(F,w)});
		

		t.shadehidden(E,F);
	},
	shadeshow: function(D,W){
		var t = this;
		var _d = document.createElement("div"), _f = document.createElement("iframe");
		var d = t.g(D), ar = t.bodysize();
		_d.setAttribute("id","shadowbox");
		_d.className = "shadowbox";
		_d.style.cssText = "width:"+ar[0]+"px;height:"+ar[1]+"px;";
		_f.style.cssText = "position:absolute;z-index:-1;width:0px;height:0px;border:none;_width:"+ar[0]+"px;_height:"+ar[1]+"px;filter:alpha(opacity=0);";
		_d.appendChild(_f);
		if (t.g("#shadowbox"))
			t.g("#shadowbox").style.visibility = "visible";
		else
			document.body.appendChild(_d);
		d.style.cssText = "overflow:hidden;width:"+W+"px;height:auto;left:"+(ar[0]-W)/2+"px;top:60px;visibility:visible;";
	},
	shadehidden: function(D,E) {
		var t = this;
		if (D.indexOf(",") == -1) {
			t.addEvent(D,"click",function() {
				fn();
			});
		} else {
			var ar = D.split(","), i = 0; j = ar.length;
			for ( ; i < j ; i ++ )
			{
				t.addEvent(ar[i],"click",function() {
					fn();
				});
			}
		};
		function fn() {
			t.g(E).style.cssText = "overflow:hidden;visibility:hidden;";
			var d = t.g("#shadowbox");
			d.parentNode.removeChild(d);
		};
	},
	inputvalue: function(D,E,F,fun) {
		var t = this;
		if ( !t.g(D) || !t.g(E) || !F) {
			return;
		};
		t.addEvent(D,"click",function(){
			var e = t.g(E), i = 0, j = e.length, st = "";
			if ( F.indexOf(",") == -1) {
				for ( ; i < j ; i ++ ) {
					st += (e[i].type == "checkbox" && e[i].id != F && e[i].checked == true ) ? e[i].value + "|" : "";
				}
			}
			else {
				var ar = F.split(",");
				var k = 0, l = ar.length, a = "";
				for ( ; k < l ; k ++ ){
					a += (ar[k] + "|");
				}
				var re = eval("(" +"/"+t.cut(a,0,-1)+"/"+")");
				for ( ; i < j ; i ++ ) {
					st += (e[i].type == "checkbox" && !re.exec(e[i].id) && e[i].checked == true ) ? e[i].value + "|" : "";
				}
			}
			fun(t.cut(st,0,-1));
		});
	}, 
	checkhandle: function(D,E,F) {
		var t = this;
		var d = t.g(D), e = t.g(E), f = t.g(F), a = t.cut(E,1,E.length), i = 0, j = d.length;
		for (; i < j; i ++ ) {
			d[i]._p = i;
			d[i].onclick = function() {
				if ( !(this.checked)) {
						e.checked = false;
						return;
				}
				checkall();
			};
			e.onclick = function() {
				var k = 0;
				if (this.checked) {
					for ( ; k < j ; k ++ ) {
						if ( d[k].type == "checkbox" && d[k].id != a) {
							d[k].checked = true;
						}
					}
				} else {
					for ( ; k < j ; k ++ ) {
						if ( d[k].type == "checkbox" && d[k].id != a) {
							d[k].checked = false;
						}
					}
				}
			};
			t.g(F).onclick = function() {
				var k = 0;
				for ( ; k < j ; k ++ ) {
					if ( d[k].type == "checkbox" && d[k].id != a ) {
						d[k].checked = ( d[k].checked == true) ? false : true;
					}
				} 
				checkall();
			};
			function checkall() {
				var k = 0;
				for ( ; k < j ; k ++ )
				{
					if ( d[k].type == "checkbox" && d[k].id != a && !(d[k].checked)) {
						e.checked = false;
						return;
					}
				}
				e.checked = true;
			}
		}
	},
	getPos: function(D) {
		var t = this;
		var d = t.g(D), x, y;  // x = y = 0
		var w = d.offsetWidth, h = d.offsetHeight;
		if( typeof( d.offsetParent ) != 'undefined' ) {
			for( var posX = 0, posY = 0; d; d = d.offsetParent ) {
				posX += d.offsetLeft;
				posY += d.offsetTop;      
			}
			x = posX + w;
			y = posY + h;
			return [ posX,posY,x,y];
		} else{
			x = d.x + w;
			y = d.y + h;
			return [ d.x,d.y,x,y];
		}
	},
	layer: function(D,E) {
		var t = this;
		var d = t.g(D), e = t.g(E), p = arguments;
		var o = (p[2] && isNaN(p[2])) ? p[2] : "mouseover";
		var w = (p[2] && !isNaN(p[2])) ? p[2] : ((p[3] && !isNaN(p[3])) ? p[3] : 106);
		t.addEvent(D,o,function() {
			var ar = t.getPos(D);
			e.style.cssText = "width:"+w+"px;border:1px solid #2983d2;position:absolute;display:block;top:"+(ar[3]-1)+"px;left:"+(ar[0]-8)+"px;_left:"+(ar[0]-15)+"px;";
		});
		t.addEvent(E,"mouseover",function() {
			var ar = t.getPos(D);
			e.style.cssText = "width:"+w+"px;border:1px solid #2983d2;position:absolute;display:block;top:"+(ar[3]-1)+"px;left:"+(ar[0]-8)+"px;_left:"+(ar[0]-15)+"px;";
		});
		t.addEvent(D,"mouseout",function() {
			e.style.cssText = "display:none";
		});
		t.addEvent(E,"mouseout",function() {
			e.style.cssText = "display:none";
		});
	},
	nodeMove: function (D){
		var d = this.g(D)
		for (var i = 0; i < d.length ; i++ )
		{
			if (d[i].className == "nodeup")
			{
				d[i].onclick = function() {
					 moveUp(this)
				}
			}
			else
			{
				d[i].onclick = function() {
					 moveDown(this)
				}
			}
		}
		function moveUp(d){
			var _row=d.parentNode.parentNode;
			if(_row.previousSibling)
				swapNode(_row,_row.previousSibling);
		}
		function moveDown(d){
			var _row=d.parentNode.parentNode;
			if(_row.nextSibling)
				swapNode(_row,_row.nextSibling);
		}
		function swapNode(node1,node2){
			var _parent=node1.parentNode;
			var _t1=node1.nextSibling;
			var _t2=node2.nextSibling;
			if(_t1)
				_parent.insertBefore(node2,_t1);
			else 
				_parent.appendChild(node2);
			if(_t2)
				_parent.insertBefore(node1,_t2);
			else 
				_parent.appendChild(node1);
		}
	}
};
var $$ = new AR();
